package com.infodms.dms.actions.sales.marketmanage.plancommon;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.actions.sysmng.usemng.SgmDealerSysUser;
import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.common.CommonDAO;
import com.infodms.dms.dao.sales.marketmanage.planmanage.ActivitiesSummaryReportDao;
import com.infodms.dms.dao.sales.planmanage.SpacePlanManageDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmDealerPO;
import com.infodms.dms.po.TmOrgPO;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

/**
 * @Title: CHANADMS
 *
 * @Description:
 *
 * @Copyright: Copyright (c) 2010
 *
 * @Company:  www.infoservice.com.cn
 * @Date: 2010-7-13
 *
 * @author zjy 
 * @mail   zhaojinyu@infoservice.com.cn
 * @version 1.0
 * @remark 
 */
public class MarketDealerTree {
	public Logger logger = Logger.getLogger(SgmDealerSysUser.class);
	/**
	 * 构造市场活动选择经销商树形菜单
	 * @param null
	 * @return void
	 * @throws Exception
	 */
	public void initOrgTree() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			act.getResponse().setContentType("application/json");
			//String rootId = request.getParamValue("tree_root_id"); // 得到树的根节点ID
			String orgId= request.getParamValue("ORGID");
			//ActivitiesSummaryReportDao dao  = ActivitiesSummaryReportDao.getInstance();
			List<TmOrgPO> funList = CommonDAO.createDeptTree(GetOemcompanyId.getOemCompanyId(logonUser), orgId,logonUser.getUserType(),true);
			act.setOutData("funlist", funList);
		} catch(BizException e){
			logger.error(logonUser,e);
			act.setException(e);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"SGM维护经销商用户");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 查询市场活动的经销商
	 * @param null
	 * @return void
	 * @throws Exception
	 */
	public void allDrlQuery() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			act.getResponse().setContentType("application/json");
			String dcode = request.getParamValue("DRLCODE");//经销代码
			String dsname = request.getParamValue("DELSNAME");//经销商名称
			String province = request.getParamValue("DRLPROVINCE");//所属省份
			String inputOrgId = request.getParamValue("ORGID");//树形选择的组织ID
			String dealerLevel = request.getParamValue("DEALERLEVEL");//由页面控制的经销商等级
			String campaignId = request.getParamValue("campaignId");//活动方案ID
			//DUTY_TYPE_DEPT
			int page_size = Constant.PAGE_SIZE;
			page_size = 20;
			ActivitiesSummaryReportDao dao  = ActivitiesSummaryReportDao.getInstance();
			Integer curPage = request.getParamValue("curPage") != null ? Integer.parseInt(request.getParamValue("curPage")): 1; // 处理当前页
			Integer poseBusType =logonUser.getPoseBusType();
			PageResult<Map<String, Object>> ps = dao.getAllDRLByDeptId(dcode,dsname,province,GetOemcompanyId.getOemCompanyId(logonUser), page_size,curPage,inputOrgId,poseBusType,campaignId);  
			act.setOutData("ps", ps);

		} catch(BizException e){
			logger.error(logonUser,e);
			act.setException(e);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"SGM维护经销商用户");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	
	/**
	 * 查询市场活动的经销商
	 * @param null
	 * @return void
	 * @throws Exception
	 */
	public void getAllDrlQuery() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			act.getResponse().setContentType("application/json");
			String areaId=request.getParamValue("areaId");//获取业务范围
			String dcode = request.getParamValue("DRLCODE");//经销代码
			String dsname = request.getParamValue("DELSNAME");//经销商名称
			String province = request.getParamValue("DRLPROVINCE");//所属省份
			String inputOrgId=String.valueOf(logonUser.getOrgId());
			//String inputOrgId = request.getParamValue("ORGID");//树形选择的组织ID
			//String dealerLevel = request.getParamValue("DEALERLEVEL");//由页面控制的经销商等级
//			String areaId=request.getParamValue("areaId");
//			if(MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).size()>0){
//				areaId=MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).get(0).get("AREA_ID").toString();
//				}
			String campaignId = request.getParamValue("campaignId");//活动方案ID
			//DUTY_TYPE_DEPT
			int page_size = Constant.PAGE_SIZE;
			page_size = 20;
			ActivitiesSummaryReportDao dao  = ActivitiesSummaryReportDao.getInstance();
			Integer curPage = request.getParamValue("curPage") != null ? Integer.parseInt(request.getParamValue("curPage")): 1; // 处理当前页
			Integer poseBusType =logonUser.getPoseBusType();
			PageResult<Map<String, Object>> ps = dao.getMyAllDRLByDeptId(areaId,dcode,dsname,province,GetOemcompanyId.getOemCompanyId(logonUser), page_size,curPage,inputOrgId,poseBusType,campaignId);  
			act.setOutData("ps", ps);

		} catch(BizException e){
			logger.error(logonUser,e);
			act.setException(e);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"SGM维护经销商用户");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 查询所有区域
	 * @param null
	 * @return void
	 * @throws Exception
	 */
	public void allOrgQuery() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			act.getResponse().setContentType("application/json");
//			String dcode = request.getParamValue("DRLCODE");//经销代码
//			String dsname = request.getParamValue("DELSNAME");//经销商名称
//			String province = request.getParamValue("DRLPROVINCE");//所属省份
//			String inputOrgId = request.getParamValue("ORGID");//树形选择的组织ID
//			String dealerLevel = request.getParamValue("DEALERLEVEL");//由页面控制的经销商等级
//			String campaignId = request.getParamValue("campaignId");//活动方案ID
			String orgName=request.getParamValue("ORGNAME");//区域名称
			//DUTY_TYPE_DEPT
			int pagesize = Constant.PAGE_SIZE;
			SpacePlanManageDao dao  = SpacePlanManageDao.getInstance();
			Integer curPage = request.getParamValue("curPage") !=null ? Integer.parseInt(request.getParamValue("curPage")) : 1;			Integer poseBusType =logonUser.getPoseBusType();
			PageResult<Map<String, Object>> ps = dao.getAllDRLByDeptId(GetOemcompanyId.getOemCompanyId(logonUser), pagesize,curPage,poseBusType,orgName);  
			act.setOutData("ps", ps);

		} catch(BizException e){
			logger.error(logonUser,e);
			act.setException(e);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"SGM维护经销商用户");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}

}
